package com.dht.xiao.jianye.base;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.base.util.GsonUtil;
import com.dht.xiao.jianye.bean.DropBean;
import com.dht.xiao.jianye.bean.GetFacilityBean;
import com.dht.xiao.jianye.utils.Information;
import com.licheedev.myutils.LogPlus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.dlc.commonlibrary.ui.base.BaseCommonActivity;
import cn.dlc.commonlibrary.utils.PrefUtil;

public abstract class BaseActivity extends BaseCommonActivity {
    private Toast mToast;
    //需要处理全屏逻辑之类的操作,在子类重写beforeSetContentView方法做处理
    @Override
    protected void beforeSetContentView() {
        super.beforeSetContentView();
    }

    //需要在每个Activity处理逻辑,直接在onCreate方法处理
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public void showOneToast(String content) {
        TextView textView;
        if (mToast == null) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_tips, null);
            textView = layout.findViewById(R.id.tv_content);
            textView.setText(content);
            mToast = new Toast(this);
            mToast.setGravity(Gravity.CENTER | Gravity.FILL, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setView(layout);
        } else {
            textView = mToast.getView().findViewById(R.id.tv_content);
            textView.setText(content);
        }
        mToast.show();
    }
//
//    public List<DropBean> getBoxData(){
//        if (Information.boxDataList == null) {
//             Integer[] Images = {R.drawable.com_home_ic_metal, R.drawable.com_home_ic_plastic,
//                     R.drawable.com_home_ic_paper, R.drawable.home_ic_clothes,
//                    R.drawable.com_home_ic_harmful};
//             String[] Names = {"瓶子","金属", "塑料", "纸类", "纺织品", "有害垃圾"};
//             double[] Prices = {0.2, 0.2, 0.2, 0.2, 0,0.2};
//             Integer[] Types = {0,3, 1, 4, 2};
//            List<DropBean> DropBeanList = new ArrayList<>();
//            List<String[]> boxStateBeanList = new ArrayList<>();
//            String[] boxState = {"0","0","0","0","0","0","0","0",
//                    "0","0","0","0","0","0","0","0"};
//            for (int i = 0; i < Images.length; i++) {
//                DropBean bean = new DropBean(Images[i], Names[i], Prices[i], Types[i],boxState,true);
//                DropBeanList.add(bean);
//
//                boxStateBeanList.add(boxState);
//            }
//            Information.boxDataList = DropBeanList;
//            Information.boxStateBeanList = boxStateBeanList;
//        }
//        return Information.boxDataList;
//
//    }
    public List<DropBean> setBoxData(){
        if (Information.boxDataList != null) {
//            Integer[] Images = {R.drawable.com_home_ic_metal, R.drawable.com_home_ic_plastic,
//                    R.drawable.com_home_ic_paper, R.drawable.home_ic_clothes,
//                    R.drawable.com_home_ic_harmful};
//            String[] Names = {"瓶子","金属", "塑料", "纸类", "纺织品", "有害垃圾"};
//            double[] Prices = {0.2, 0.2, 0.2, 0.2, 0,0.2};
//            Integer[] Types = {0,3, 1, 4, 2};
//            List<DropBean> DropBeanList = new ArrayList<>();

            for (int i = 0; i < Information.boxDataList.size(); i++) {
                Information.boxDataList.get(i).setBoxState(Information.boxStateBeanList.get(i));
               // LogPlus.e("更改箱子状态="+ Arrays.toString(Information.boxStateBeanList.get(i)));
            }


        }else {
            return getBoxData();
        }
        return Information.boxDataList;
    }

    private MediaPlayer mediaPlayer;

    public void playMusic(int resId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();
                }
                mediaPlayer.reset();
                mediaPlayer.stop();
                AssetFileDescriptor file = getResources().openRawResourceFd(resId);
                try {
                    mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
                            file.getLength());
                    mediaPlayer.prepareAsync();
                    file.close();
                    mediaPlayer.setOnPreparedListener(mp -> {
                        mediaPlayer.start();
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.setOnCompletionListener(mp -> {
                    mediaPlayer.reset();
                });
                mediaPlayer.setOnErrorListener((mp, what, extra) -> {
                    Log.e("))))))))))", "mediaPlayerError" + what);
                    return true;
                });
            }
        });
    }
    public void stopPlay() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
    public List<DropBean> getBoxData(){
        if (Information.boxDataList == null) {
//             Integer[] Images = {R.drawable.com_home_ic_metal, R.drawable.com_home_ic_plastic,
//                     R.drawable.com_home_ic_paper, R.drawable.home_ic_clothes,
//                    R.drawable.com_home_ic_harmful};
//             String[] Names = {"瓶子","金属", "塑料", "纸类", "纺织品", "有害垃圾"};
//             double[] Prices = {0.2, 0.2, 0.2, 0.2, 0,0.2};
//             Integer[] Types = {0,3, 1, 4, 2};
            String string =  PrefUtil.getDefault().getString(Information.facilitySetting,"");
            LogPlus.e("数据="+string);
            try {
                GetFacilityBean mGetFacilityBean = GsonUtil.getInstance().parseJsonStrToObj(string,GetFacilityBean.class);
                Information.facilityinfo = mGetFacilityBean;
                List<DropBean> DropBeanList = new ArrayList<>();
                List<String[]> boxStateBeanList = new ArrayList<>();
                String[] boxState = {"0","0","0","0","0","0","0","0",
                        "0","0","0","0","0","0","0","0"};
                for (int i = 0; i < mGetFacilityBean.data.facilityCabinets.size(); i++) {
                    GetFacilityBean.DataBean.FacilityCabinetsBean dataBean = mGetFacilityBean.data.facilityCabinets.get(i);
                        try{
                            DropBean bean = new DropBean(dataBean.cabinetName,
                                    Double.valueOf(dataBean.deliverPrice), Integer.valueOf(dataBean.cabinetNumber), boxState, true, dataBean.isOpen, dataBean.isCount);
                            DropBeanList.add(bean);
                            boxStateBeanList.add(boxState);
                        }catch (Exception e){
                            LogPlus.e(e.getLocalizedMessage());
                        }


                }
                Information.boxDataList = DropBeanList;
                Information.boxStateBeanList = boxStateBeanList;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return Information.boxDataList;

    }


}
